var http=require('http');
var Promise=require('bluebird');
var cheerio=require('cheerio');
var baseUrl='http://www.tuicool.com';
var url="http://www.tuicool.com/ah";

function filterChapters(html){
  var $=cheerio.load(html);
  var chapters=$('.single_fake');
  var title=$('.new-nav-ul .active a').text();
  var courseData={
    title:title,
    articles:[],

  }
  chapters.each(function(item){
    var chapter=$(this);
    var chapterTitle=chapter.find('.article_title  a').text();
    var id=chapter.attr('data-id');
    // var description=chapter.find('strong').text();
    var chapterData={
      chapterTitle:chapterTitle,
      id:id,
      // description:description
    }
    courseData.articles.push(chapterData);
  })

  return courseData;
}

function printCourseInfo(coursesData){
  coursesData.forEach(function(courseData){
    console.log(courseData.title+'\n')
    courseData.articles.forEach(function(article){
      console.log('-----['+article.id+']'+article.chapterTitle+'\n');
    })
  })
}


function getPageAsync(url){
  return new Promise(function(resolve, reject) {
    console.log('正在爬取'+url);
    http.get(url,function(res){
      var html='';
      res.on('data',function(data){
        html+=data;
      })

      res.on('end',function(){
        resolve(html);
      })
    }).on('error',function(e){
      reject(e)
      console.log('获取课程出错!');
    })
  });
}


function run(){
  http.get(url,function(res){
    var html='';
    res.on('data',function(data){
      html+=data;
    })

    res.on('end',function(){
      getarticles(getUrl(html));
    })
  }).on('error',function(e){
    reject(e)
    console.log('获取课程出错!');
  })
}

function getUrl(html){
  var urls=[];
  var $=cheerio.load(html);
  var chapters=$('.new-nav-ul a');
  chapters.each(function(item){
    urls.push($(this).attr('href'));
  })
  return urls;
}



function getarticles(urls){
  var fetchCourseArray=[]
  urls.forEach(function(id){
    fetchCourseArray.push(getPageAsync(baseUrl+id));
  })

  Promise
    .all(fetchCourseArray)
    .then(function(pages){
      var coursesData=[];
      pages.forEach(function(html){
        var courses=filterChapters(html);

        coursesData.push(courses);
      })

      printCourseInfo(coursesData);
    })

}

run();
